package zhuyun.code

import java.awt.GridLayout
import java.awt.Label
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.util.*
import javax.swing.*

fun main(args:Array<String>){
    DatingGameWin()
}

class You{
    lateinit var wifeName:String
    fun createMemento():Girl{
        return Girl(wifeName)
    }
    fun restoreMemento(p:Girl?){
        p?.let { wifeName = it.name }

    }
}
class Girl(val name:String)
class GirlStack{
    private val girls = Stack<Girl>()
    fun push(p:Girl){
        girls.push(p)
    }
    fun pop():Girl?{
        if(!girls.empty()){
            return girls.pop()
        }
        return null

    }
}
class DatingGameWin:JFrame(),ActionListener{
    private lateinit var girls:GirlStack
    private lateinit var you:You
    private lateinit var eastJP:JPanel
    private lateinit var g:JLabel
    private lateinit var button1:JButton
    private lateinit var button2:JButton
    private lateinit var girl1:JRadioButton
    private lateinit var girl2:JRadioButton
    private lateinit var girl3:JRadioButton
    private lateinit var girl4:JRadioButton
    init {
        title = "利用备忘录模式设计相亲游戏"
        you = You()
        girls = GirlStack()
        this.setBounds(0,0,1000,380)
        this.isResizable = false
        val fileName = "src/images/四大美女.jpg"
        g = JLabel(ImageIcon(fileName),JLabel.CENTER)
        val centerJP = JPanel()
        centerJP.layout = GridLayout(1,4)
        centerJP.border = BorderFactory.createTitledBorder("四大美女如下:")
        centerJP.add(g)
        this.add("Center", centerJP)
        eastJP = JPanel()
        eastJP.layout = GridLayout(1,1)
        eastJP.border = BorderFactory.createTitledBorder("你选择的爱人是:")
        this.add("East",eastJP)
        val southJP = JPanel()
        val info = JLabel("四大美女有“沉鱼落雁之容、闭月羞花之貌”，您选择谁？")
        girl1 = JRadioButton("西施",true)
        girl2 = JRadioButton("貂蝉")
        girl3 = JRadioButton("王昭君")
        girl4 = JRadioButton("杨玉环")
        button1 = JButton("确定")
        button2 = JButton("返回")
        val group = ButtonGroup()
        group.add(girl1)
        group.add(girl2)
        group.add(girl3)
        group.add(girl4)
        southJP.add(info)
        southJP.add(girl1)
        southJP.add(girl2)
        southJP.add(girl3)
        southJP.add(girl4)
        southJP.add(button1)
        southJP.add(button2)
        button1.addActionListener(this)
        button2.addActionListener(this)
        this.add("South",southJP)
        showPicture("西施")
        you.wifeName = "西施"
        girls.push(you.createMemento())



    }

    fun showPicture(name:String){
        eastJP.removeAll()
        eastJP.repaint()
        you.wifeName = name
        val fileName = "src/images/$name.jpg"
        g = JLabel(ImageIcon(fileName),JLabel.CENTER)
        eastJP.add(g)
        this.isVisible = true
        this.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
    }
    override fun actionPerformed(e: ActionEvent?) {
        when(e!!.source){
            button1->{
                girls.push(you.createMemento())
                if(girl1.isSelected){
                    showPicture("西施")
                } else {
                    if(girl2.isSelected){
                        showPicture("貂蝉")
                    } else {
                        if (girl3.isSelected){
                            showPicture("王昭君")
                        } else {
                            showPicture("杨玉环")
                        }
                    }
                }
            }
            button2->{
                you.restoreMemento(girls.pop())
                showPicture(you.wifeName)
            }
        }

    }

}
